﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClusHal
{
    class OWADistance:IDistanceMeasure 
    {
        public static double[] weights=null;

        #region IDistanceMeasure Members

        public double Distance(Action a1, Action a2)
        {
            if (weights == null)
                throw new Exception("Missing weight vector");

            if (a1.Size == a2.Size)
            {
                List<double> distances = new List<double>();
                for (int i=0;i<a1.Size;i++)
                {
                    if (Action.ValidCriterion(i))
                    {
                        double d = Math.Abs(a1[i] - a2[i]);
                        distances.Add(d);
                    }
                }
                distances.Sort();

                if (distances.Count != weights.Length)
                    throw new Exception("Incorrect weight vector size");

                double sum = 0;
                for (int i = 0; i < weights.Length; i++)
                {
                    sum += weights[i] * distances[i];
                }
                return sum;
            }
            else
                throw new Exception("Incorrect action sizes");
        }

        #endregion
    }
}
